--[[
  客户端的请求通过一些代理转发之后，4层的源地址会发生变化，此时需要重新提取原有的客户端IP并设置到源地址中去
  此部操作是用来定位用户的地理位置
]]
require("apm_core")
require("ApmXml")

function post_analysis()
    local ApmData = require("ApmData")
    if ApmData.getL7Proto() ~= 80 then return end

    -- X-Forwarded-For: client, proxy1, proxy2
    local s_x_forwarded_for = ApmData.getField("request.http.header.x-forwarded-for", 1)
    if s_x_forwarded_for ~= nil then
        local i_index = s_x_forwarded_for:find(",")
        s_x_forwarded_for = s_x_forwarded_for:sub(1, i_index-1)
        ApmData.setSrcIp(s_x_forwarded_for)
    end

    local s_title = ApmData.getField("transaction.title", 1)
    if s_title == nil then return end
    if string.find(s_title, "%.htm") ~= nil or string.find(s_title, "%.jsp") ~= nil or string.find(s_title, "/FrontController") ~= nil then
        ApmData.replaceField("transaction.type_code", s_title .. " ")
        ApmData.replaceField("transaction.type_name", s_title .. " ")
        logInfo("ESB(%s:%d->%s:%d) title:[%s]",
            ApmData.getSrcIp(),
            ApmData.getSrcPort(),
            ApmData.getDstIp(),
            ApmData.getDstPort(),
            s_title .. " ")
    end

    return
end
